﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Net;

namespace DynDns
{

    //ONLINE:
    // default.aspx?username=root&password=x7ax510G&hostname=home.fly-air.de&myip=5.28.105.63

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // load from Config File
            ChangeConfigValue ccV = new ChangeConfigValue();
            
            // Set NEW IP, read User and pass from web.config
            if (Request.QueryString["username"] == ccV.readIP("dnsUser") & Request.QueryString["password"] == ccV.readIP("dnsPass"))
            {
                if( (Request.QueryString["hostname"] != null) && (Request.QueryString["myip"] != null) )
                {
                    ccV.changeHost(Request.QueryString["hostname"]);                                // update server name
                    ccV.changeIP(Request.QueryString["hostname"], Request.QueryString["myip"]);     // update ip
                }
                else 
                {
                    string ipAdress = getIP();
                    // IP Adresse vom Request
                    if (Request.QueryString["hostname"] != null && ipAdress != "") 
                    {
                        ccV.changeHost(Request.QueryString["hostname"]);                            // update server name
                        ccV.changeIP(Request.QueryString["hostname"], ipAdress);                    // update ip                                     
                    }                
                }             
                PanelGui.Visible = false;                
            }
            else {
                Response.Redirect("login.aspx");
                LabelHost.Text = ServerHostName();
                PanelGui.Visible = true;
            }
        }   

        // get IP Adress from USER
        protected string getIP()
        {
            // Get request.
            HttpRequest request = base.Request;
            string address = request.UserHostAddress;
            return address;
        }

        /// <summary>
        /// method to get Client ip address
        /// </summary>
        /// <param name="GetLan"> set to true if want to get local(LAN) Connected ip address</param>
        /// <returns></returns>
        public static string GetVisitorIPAddress(bool GetLan = false)
        {
            string visitorIPAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            if (String.IsNullOrEmpty(visitorIPAddress))
                visitorIPAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

            if (string.IsNullOrEmpty(visitorIPAddress))
                visitorIPAddress = HttpContext.Current.Request.UserHostAddress;

            if (string.IsNullOrEmpty(visitorIPAddress) || visitorIPAddress.Trim() == "::1")
            {
                GetLan = true;
                visitorIPAddress = string.Empty;
            }

            if (GetLan && string.IsNullOrEmpty(visitorIPAddress))
            {
                //This is for Local(LAN) Connected ID Address
                string stringHostName = Dns.GetHostName();
                //Get Ip Host Entry
                IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName);
                //Get Ip Address From The Ip Host Entry Address List
                IPAddress[] arrIpAddress = ipHostEntries.AddressList;

                try
                {
                    visitorIPAddress = arrIpAddress[arrIpAddress.Length - 2].ToString();
                }
                catch
                {
                    try
                    {
                        visitorIPAddress = arrIpAddress[0].ToString();
                    }
                    catch
                    {
                        try
                        {
                            arrIpAddress = Dns.GetHostAddresses(stringHostName);
                            visitorIPAddress = arrIpAddress[0].ToString();
                        }
                        catch
                        {
                            visitorIPAddress = "127.0.0.1";
                        }
                    }
                }
            }
            return visitorIPAddress;
        }

        // get host adress
        public static string ServerHostName()
        {
            string port = HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
            if (port == null || port == "80" || port == "443"){
                port = "";
            }else {
                port = ":" + port;
            }
     
            string protocol = HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
            if (protocol == null || protocol == "0") {
                protocol = "http://";
            } else {
                protocol = "https://";
            }
     
            return protocol + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + port;
        }
    }

}